package sq.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sq.dao.Cart;
import sq.dao.Food;
import sq.dao.User;
import sq.repository.CartRepository;
import sq.repository.FoodRepository;
import sq.repository.UserRepository;
import sq.service.CartService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: sunquan
 * @Date: 2018/3/5
 * @Time: 14:29
 */
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private FoodRepository foodRepository;

    @Override
    public String addFoodByCart(Integer foodId, double price, Integer buyNumber, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Cart cart = new Cart();
        try {
            String userName = session.getAttribute("userSession").toString();
            User user = userRepository.findUserByUsername(userName);
            Integer userId = user.getUserId();
            cart.setUserId(userId);
        } catch (NullPointerException e) {
            e.printStackTrace();
            return "1";
        }
        Food food=foodRepository.findOne(foodId);
        cart.setFoodId(foodId);
        cart.setBuyNumber(buyNumber);
        cart.setTotalPrice(price * buyNumber);
        cart.setLastUpdate(new Date());
        cart.setPrice(price);
        cart.setFoodName(food.getFoodName());
        cart.setFoodImg(food.getFoodImg());
        cartRepository.save(cart);
        return "0";
    }

    @Override
    public List<Cart> findCart(HttpServletRequest request) {
        HttpSession session = request.getSession();
        try {
            String userName = session.getAttribute("userSession").toString();
            User user = userRepository.findUserByUsername(userName);
            Integer userId = user.getUserId();
            List<Cart> cartList = cartRepository.findAllByUserId(userId);
            return cartList;
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Transactional
    public void deletCart(int cartId) {
        cartRepository.delete(cartId);
    }

    @Override
    @Transactional
    public void deleteAllCart(String checkedId,HttpServletRequest request) {
        String[] ids=checkedId.split(",");
        for(int i=0;i<ids.length;i++){
            int id = Integer.parseInt(ids[i]);
            cartRepository.delete(id);
        }
    }

    @Override
    public void delCart(String cartId) {
        String[] ids = cartId.split(",");
        for (int i = 0; i < ids.length; i++) {
            int id = Integer.parseInt(ids[i]);
            cartRepository.delete(id);
        }

    }


}
